package com.yf1706.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yf1706.bean.FloatSalary;
import com.yf1706.bean.FloatSalarySob;
import com.yf1706.dao.jpa.IFloatSalaryJDAO;
import com.yf1706.dao.jpa.IFloatSalarySobJDAO;
import com.yf1706.vo.FloatSalaryVO;

/**
 * 浮动工资服务
 * @author 锦涛
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class FloatSalaryService {

	@Autowired
	private IFloatSalarySobJDAO fSalarySobDAO;
	
	@Autowired
	private IFloatSalaryJDAO fSalaryDAO;
	//--------------------浮动工资账套-------------------------
	/**
	 * 添加浮动工资账套
	 */
	@Transactional
	public void saveSob(FloatSalarySob floatSalarySob) {
		fSalarySobDAO.save(floatSalarySob);
	}
	
	/**
	 * 删除浮动工资账套
	 */
	@Transactional
	public void removeSob(String ids) {
		for(String id : ids.split(",") ) {
			if ( id != null) {
				fSalarySobDAO.deleteById(Integer.parseInt(id));
			}
		}
	}
	
	/**
	 * 修改浮动工资账套
	 */
	@Transactional
	public void updateSob(FloatSalarySob floatSalarySob) {
		Optional<FloatSalarySob> SfloatSalarySob = fSalarySobDAO.findById(floatSalarySob.getFloatSalarySobId());
		SfloatSalarySob.get().setFloatSalarySobName(floatSalarySob.getFloatSalarySobName());
		SfloatSalarySob.get().setRemark(floatSalarySob.getRemark());
	}
	
	/**
	 * 分页查询浮动工资账套
	 */
	@Transactional
	public Page<FloatSalarySob> querySobByPage(int page,int rows,String sort,String order){
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return fSalarySobDAO.findAll(pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return fSalarySobDAO.findAll(pageable);
		}
	}
	
	/**
	 * 模糊分页查询浮动工资账套
	 */
	@Transactional
	public Page<FloatSalarySob> findSearch(String floatSalarySobName,int page,int rows,String sort,String order) {
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return fSalarySobDAO.findSearch(floatSalarySobName, pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return fSalarySobDAO.findSearch(floatSalarySobName, pageable);
		}
	}
	
	/**
	 * 查询所有的浮动工资账套
	 */
	public List<FloatSalarySob> querySobAll(){
		return fSalarySobDAO.findAll();
	}
	
	public FloatSalarySob findSobByID(int id) {
		return fSalarySobDAO.getOne(id);
	}
	
	//--------------------浮动工资表--------------------------
	/**
	 * 添加浮动工资
	 */
	@Transactional
	public void saveFS(FloatSalary floatSalary) {
		fSalaryDAO.save(floatSalary);
	}
	
	/**
	 * 删除浮动工资
	 */
	@Transactional
	public void removeFS(String ids) {
		for(String id : ids.split(",") ) {
			if ( id != null) {
				fSalaryDAO.deleteById(Integer.parseInt(id));
			}
		}
	}
	
	/**
	 * 修改浮动工资
	 */
	@Transactional
	public void updateFS(FloatSalary floatSalary) {
		Optional<FloatSalary> SfloatSalary = fSalaryDAO.findById(floatSalary.getFloatSalaryId());
		SfloatSalary.get().setFloatSalaryName(floatSalary.getFloatSalaryName());
		SfloatSalary.get().setValue(floatSalary.getValue());
		SfloatSalary.get().setBeginDate(floatSalary.getBeginDate());
		SfloatSalary.get().setEndDate(floatSalary.getEndDate());
		SfloatSalary.get().setMender(floatSalary.getMender());
		SfloatSalary.get().setMendDate(floatSalary.getMendDate());
		SfloatSalary.get().setRemark(floatSalary.getRemark());
	}
	
	/**
	 * 分页查询浮动工资
	 */
	@Transactional
	public Page<FloatSalaryVO> queryFSByPage(int id,int page,int rows,String sort,String order){
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return fSalaryDAO.findSearch(id, pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return fSalaryDAO.findSearch(id, pageable);
		}
	}
	
	/**
	 * 查询所有浮动工资
	 */
	public List<FloatSalary> queryFSAll(){
		return fSalaryDAO.findAll();
	}
	
}
